package com.jzsec.tzdslog;

public class OpstationUtils {

	public static void main(String[] args) {
		System.out.println(formatIp("123.1982"));
		System.out.println(formatMac("mac :\\ 11-11-11:00:34"));
		System.out.println(formatMobilephone("mobile :+8613922443355"));
		System.out.println(formatImei("imei :2345723456789"));
		System.out.println(formatVer("ver :2345723456789"));
		System.out.println(formatUdid("udid :2345723456789r\r\n\t aa"));
	}

	/**
	 * @param value
	 * @return 匹配手机号，并添加前缀
	 */
	public static String formatMobilephone(String value) {
		String flagStr = "mobile:";
		if (value != null) {
			value = value.replaceAll(" ", "");
			value = value.replaceAll("\\\\", "");
			value = value.replaceAll("　", "");
			value = value.replaceAll("\t", "");
			value = value.replaceAll("\n", "");
			value = value.replaceAll("\r", "");
			if (value.startsWith(flagStr)) {
				value = value.substring(flagStr.length());
			}
			if (value.length() > 11) {
				value = value.substring(value.length() - 11, value.length());
			}
		} else {
			value = "";
		}
		return value;
	}

	/**
	 * @param value
	 * @return 格式化IP地址
	 */
	public static String formatIp(String value) {
		String flagStr = "clientip:";
		if (value != null) {
			value = value.replaceAll(" ", "");
			value = value.replaceAll("\\\\", "");
			value = value.replaceAll("　", "");
			value = value.replaceAll("\t", "");
			value = value.replaceAll("\n", "");
			value = value.replaceAll("\r", "");
			if (value.startsWith(flagStr)) {
				value = value.substring(flagStr.length());
			}
			value = formatTo15bit(value);
		} else {
			value = formatTo15bit(value);
		}
		return value;
	}

	public static String formatTo15bit(String ip) {
		if(null == ip) ip = "";
		for(int i = ip.length(); i < 15; ++i) {
			ip += " ";
		}
		return ip;
	}

	/**
	 * @param value
	 * @return 将mac地址格式化为00-30-67-e7-94-47 格式
	 */
	public static String formatMac(String value) {
		String flagStr = "mac:";
		if (value != null) {
			value = value.replaceAll(" ", "");
			value = value.replaceAll("\\\\", "");
			value = value.replaceAll("　", "");
			value = value.replaceAll("\t", "");
			value = value.replaceAll("\n", "");
			value = value.replaceAll("\r", "");
			if (value.startsWith(flagStr)) {
				value = value.substring(flagStr.length());
			}
			value = value.replaceAll(":", "-");
		} else {
			value = "";
		}
		return value;
	}

	/**
	 * @param value
	 * @return 匹配imei，并添加前缀
	 */
	public static String formatImei(String value) {
		String flagStr = "imei:";
		if (value != null) {
			value = value.replaceAll(" ", "");
			value = value.replaceAll("\\\\", "");
			value = value.replaceAll("　", "");
			value = value.replaceAll("\t", "");
			value = value.replaceAll("\n", "");
			value = value.replaceAll("\r", "");
			if (value.startsWith(flagStr)) {
				value = value.substring(flagStr.length());
			}
		} else {
			value = "";
		}
		return value;
	}

	/**
	 * @param value
	 * @return 匹配版本号ver，并添加前缀
	 */
	public static String formatVer(String value) {
		String flagStr = "ver:";
		if (value != null) {
			value = value.replaceAll(" ", "");
			value = value.replaceAll("\\\\", "");
			value = value.replaceAll("　", "");
			value = value.replaceAll("\t", "");
			value = value.replaceAll("\n", "");
			value = value.replaceAll("\r", "");
			if (value.startsWith(flagStr)) {
				value = value.substring(flagStr.length());
			}
			value = flagStr + value;
		} else {
			value = flagStr;
		}
		return value;
	}

	/**
	 * @param value
	 * @return 匹配uuid，并添加前缀
	 */
	public static String formatUdid(String value) {
		String flagStr = "udid:";
		if (value != null) {
			value = value.replaceAll(" ", "");
			value = value.replaceAll("\\\\", "");
			value = value.replaceAll("　", "");
			value = value.replaceAll("\t", "");
			value = value.replaceAll("\n", "");
			value = value.replaceAll("\r", "");

			if (value.startsWith(flagStr)) {
				value = value.substring(flagStr.length());
			}
		} else {
			value = "";
		}
		return value;
	}
}
